package com.acg.auth.controller;


import com.acg.auth.pojo.SysUser;
import com.acg.auth.service.SysUserService;
import com.acg.auth.vo.SysUserVo;
import com.acg.util.MD5;
import com.acg.util.OperLog;
import com.acg.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author togogo
 * @since 2025-07-28
 */
@RestController
@RequestMapping("/acg/user")
@CrossOrigin("*")
public class SysUserController {

    @Autowired
    private SysUserService service;

    /**
     * 用户条件分页查询
     * @return
     */
    @GetMapping("/getPage/{page}/{limit}")
    public Result getPage(@PathVariable Long page,
                          @PathVariable Long limit,
                          SysUserVo vo) {

        // 1、分页条件
        Page<SysUser> ipage = new Page<>(page, limit);

        // 2、条件判断
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        if( !ObjectUtils.isEmpty(vo.getKeyword()) ) {
            // 模糊查找用户名和昵称 和手机号码
            lqw.like(SysUser::getUsername, vo.getKeyword())
                    .or()
                    .like(SysUser::getName, vo.getKeyword())
                    .or()
                    .like(SysUser::getEmail, vo.getKeyword());
        }

        // 判断时间是否为空
        if (!ObjectUtils.isEmpty(vo.getCreateTimeEnd())
                && !ObjectUtils.isEmpty(vo.getCreateTimeStart())) {
            // 不用加and  默认and 条件
            lqw.ge(SysUser::getCreateTime, vo.getCreateTimeStart())
                    .le(SysUser::getCreateTime, vo.getCreateTimeEnd());
        }

        // 3 分页条件查询
        Page<SysUser> paged = service.page(ipage, lqw);
        return Result.ok(paged);
    }


    /**
     * 根据主键查询
     */
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable Long id) {
        return Result.ok(service.getById(id));
    }

    /**
     * 增加用户
     */
    @OperLog(value = "增加用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user) {
        // 把密码进行 MD5加密
        String encrypt = MD5.encrypt(user.getPassword());
        user.setPassword(encrypt);
        boolean save = service.save(user);
        if (save) {
            return Result.ok();
        }else {
            return Result.fail("增加失败");
        }
    }


    /**
     * 更新用户
     */
    @PutMapping("update")
    public Result update(@RequestBody SysUser user) {

        boolean save = service.updateById(user);
        if (save) {
            return Result.ok();
        }else {
            return Result.fail("更新失败");
        }
    }


    /**
     * 根据主键删除
     */
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean b = service.removeById(id);
        if (b) {
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids       主键列表
     * @return
     */
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> ids) {
        boolean b = service.removeByIds(ids);
        if (b) {
            return Result.ok();
        }else {
            return Result.fail("删除失败");
        }
    }

    /**
     * 修改用户状态
     * @param id        用户主键
     * @param status    状态
     * @return
     */
    @PutMapping("/updateState/{id}/{status}")
    public Result updateState(@PathVariable String id,@PathVariable Integer status) {
        System.err.println("打印"+id+status);
        boolean b = service.updateState(id, status);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail("更新失败");
        }
    }

    /**
     * 角色分配 接口
     */


}

